package rkdwogus7.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JPasswordField;

import java.awt.Dimension;

import javax.swing.JRadioButton;

import rkdwogus7.dao.MemberDAO;
import rkdwogus7.model.MemberVO;

import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;

public class UpdateMember extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField name;
	private JTextField uid;
	private JPasswordField repwd;
	private JPasswordField pwd;
	private MemberDAO mdao = new MemberDAO();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UpdateMember dialog = new UpdateMember( "0", 0, null );
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	// public UpdateMember()
	public UpdateMember( final String mno, final int r, final JTable table ) {
		// 회원 정보 수정을 위해서 ManageMember에서 전달한 mno를 받기 위해
		// UpdateMember의 기본 생성자 대신 mno를 매개 변수로 하는 새로운 생성자 작성
		// 또한, 회원 정보 수정 후 ManageMember의 JTable model도 변경해야 하므로
		// 수정해야 할 행row과 JTable 객체도 생성자 매개변수로 선언
		setTitle(":: 회원 수정 ::");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JLabel txtName = new JLabel("이름");
		txtName.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		
		name = new JTextField();
		name.setColumns(10);
		
		JLabel txtUid = new JLabel("아이디");
		txtUid.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		
		uid = new JTextField();
		uid.setEnabled(false);
		uid.setColumns(10);
		
		JLabel txtPwd = new JLabel("비밀번호");
		txtPwd.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		
		pwd = new JPasswordField();
		pwd.setPreferredSize(new Dimension(100, 21));
		
		JLabel txtGender = new JLabel("성별");
		txtGender.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		
		JRadioButton isMale = new JRadioButton("남자");
		
		JRadioButton isFemale = new JRadioButton("여자");
		isFemale.setSelected(true);
		
		JLabel txtRepwd = new JLabel("비밀번호확인");
		txtRepwd.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		
		repwd = new JPasswordField();
		repwd.setPreferredSize(new Dimension(100, 21));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(20)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtRepwd, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(repwd, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtName, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
							.addGap(66)
							.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(pwd, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtGender, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(isMale, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(isFemale, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)))
					.addGap(198))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(34)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(1)
							.addComponent(txtName, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE))
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(12)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(1)
							.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE))
						.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(15)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(1)
							.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE))
						.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(1)
							.addComponent(txtRepwd, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE))
						.addComponent(repwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(2)
							.addComponent(txtGender, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE))
						.addComponent(isMale)
						.addComponent(isFemale))
					.addContainerGap(22, Short.MAX_VALUE))
		);
		
		
		
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okBtn = new JButton("수정완료");
				okBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// 수정 완료 버튼 클릭 시
						// 1. 수정할 값을 MemberVO에 저장
						MemberVO m = new MemberVO(mno, null, new String ( pwd.getPassword() ),
								name.getText(), null, null);
						// mno Final로 선언
						// 2. MemberDAO의 UpdateMember method호출 후
						int cnt = new MemberDAO().updateMember(m);
						// 3. 비밀번호 확인 한 번 해 주고
						if ( ! Arrays.equals(pwd.getPassword(), repwd.getPassword()) ) {
							JOptionPane.showMessageDialog(null, "비밀번호가 다릅니다!");
						}
						// 4. 회원 정보가 제대로 수정된 경우 수정 완료 메시지 창 출력
						if (cnt > 0) {
							JOptionPane.showMessageDialog(null, "수정완료!");
							
							// 5. UpdateMember에서 수정한 값을 
							// ManageMember의 JTable에도 적용해야 함
							// 이름 값만 변경되기 때문에 이렇게 해 주면 됨
							table.setValueAt(name.getText(), r, 2);
							table.repaint(); // 변경사항 적용
							
							dispose(); // 현재창 닫기
						}
						
						
						
/*						if ( ! Arrays.equals(pwd.getPassword(), repwd.getPassword()) ) {
							JOptionPane.showMessageDialog(null, "비밀번호를 확인하세요!");
						}
						else {
							MemberVO n = new MemberVO(mno, null, pwd.getPassword().toString(), name.getText(), null, null);
							if (mdao.updateMember(m) > 0) {
								JOptionPane.showMessageDialog(null, "정보 수정 완료!");
							}
						}
*/ // 이건 내가 입력한 코드
					}
				});
				okBtn.setActionCommand("OK");
				buttonPane.add(okBtn);
				getRootPane().setDefaultButton(okBtn);
			}
			{
				JButton notBtn = new JButton("수정취소");
				notBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose(); 
						// 수정 취소 클릭 시 회원 정보 수정 창 닫음
					}
				});
				notBtn.setActionCommand("Cancel");
				buttonPane.add(notBtn);
			}
		}
		
		// 회원 번호를 이용해서 특정 회원의 모든 정보를 select한 후
		// MemberVO에 저장
		MemberVO m = new MemberDAO().selectOneMember(mno);
		
		// MemberVO의 값을 컴퍼넌트에 출력
		uid.setText(m.getUserid());
		name.setText(m.getName());
		pwd.setText(m.getPwd());
		
		
	} // 생성자에 걸리는 괄호
}







